﻿using Serilog;
using StudentInfoSystem.Core.Models;
using StudentInfoSystem.Core.Utils;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;

namespace StudentInfoSystem.UI.Views.Windows;

public partial class LoginWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event LoginSuccessEventHandler LoginSuccess;
    public event LoginOutEventHandler LoginOut;
    public delegate void LoginSuccessEventHandler(UserAccount user);
    public delegate void LoginOutEventHandler();
    // 添加字段记录当前登录的用户
    public UserAccount? _currentUser;
    // 添加属性标识是否在启动过程中
    public bool IsDuringStartup { get; set; } = true;

    // 保存登录信息的临时变量
    private string _lastUsername = "";
    private string _lastPassword = "";
    private readonly AppSettings appSettings = AppSettings.Instance;

    public LoginWindow()
    {
        InitializeComponent();
        this.DataContext = this; // 设置 DataContext 为自身
                                 // 确保正确获取当前用户，包括管理员状态
        if (AppSettings.Instance.CurrentUser != null)
        {
            // 从用户账户列表中获取完整的用户信息（包含IsAdmin属性）
            var currentUserFromList = AppSettings.Instance.UserAccounts
                .FirstOrDefault(u => u.Username == AppSettings.Instance.CurrentUser.Username);

            _currentUser = currentUserFromList ?? AppSettings.Instance.CurrentUser;
        }
        else
        {
            _currentUser = null;
        }
        Loaded += LoginWindow_Loaded;
    }
    // 修改 IsAutoLogin 属性
    public bool IsAutoLogin
    {
        get => AppSettings.Instance.IsAutoLogin;
        set
        {
            if (AppSettings.Instance.IsAutoLogin != value)
            {
                AppSettings.Instance.IsAutoLogin = value;
                OnPropertyChanged();
                SyncLoginOptions();
            }
        }
    }

    // 同样修改 IsRememberPassword 属性
    public bool IsRememberPassword
    {
        get => AppSettings.Instance.IsRememberPassword;
        set
        {
            if (AppSettings.Instance.IsRememberPassword != value)
            {
                AppSettings.Instance.IsRememberPassword = value;
                OnPropertyChanged();
                SyncLoginOptions();
            }
        }
    }

    private void LoginWindow_Loaded(object sender, RoutedEventArgs e)
    {
        if(IsDuringStartup)
        {
            // 启动时如果记住密码，就自动输入账号和密码
            if (IsRememberPassword)
            {
                PasswordBox.Password = AppSettings.Instance.LastPassword;
                // 恢复上次的登录信息
                UsernameTextBox.Text = AppSettings.Instance.LastUsername;
            }
        }
        else {

            // 检查是否有记住的账号
            if (AppSettings.Instance.CurrentUser != null)
            {
                // 显示用户信息面板
                ShowUserInfo();
            }
            else
            {
                // 恢复上次的登录信息
                UsernameTextBox.Text = AppSettings.Instance.LastUsername;

                // 显示登录表单
                UserInfoPanel.Visibility = Visibility.Collapsed;
                LoginCard.Visibility = Visibility.Visible;
            }
        }
        
        // 设置焦点到用户名输入框
        Dispatcher.BeginInvoke(new Action(() =>
        {
            if (LoginCard.Visibility == Visibility.Visible)
            {
                UsernameTextBox?.Focus();
            }
        }), System.Windows.Threading.DispatcherPriority.Background);
    }

    public void ShowUserInfo()
    {
        if (IsDuringStartup) return;

        if (_currentUser != null)
        {
            // 更新UI
            LoginCard.Visibility = Visibility.Collapsed;
            UserInfoPanel.Visibility = Visibility.Visible;
            AccountSelectionPanel.Visibility = Visibility.Collapsed;

            UserNameText.Text = _currentUser.Username;
            UserEmailText.Text = _currentUser.Email;

            // 根据用户类型显示不同的功能
            if (_currentUser.IsAdmin)
            {
                SwitchToRegisterButton.Visibility = Visibility.Visible;
                UserNameText.Text = $"{_currentUser.Username} (管理员)";
                LogoutFromSelectionButton.Visibility = Visibility.Collapsed;// 管理员不能注销自己
                LogoutFromUserInfoButton.Visibility = Visibility.Collapsed;
            }
            else
            {
                SwitchToRegisterButton.Visibility = Visibility.Collapsed;
                LogoutFromSelectionButton.Visibility = Visibility.Visible;
                LogoutFromUserInfoButton.Visibility = Visibility.Visible;
            }

            // 保存最后一次登录信息
            AppSettings.Instance.LastUsername = _currentUser.Username;
        }
        else
        {
            UserInfoPanel.Visibility = Visibility.Collapsed;
            LoginCard.Visibility = Visibility.Visible;
            AccountSelectionPanel.Visibility = Visibility.Collapsed;
            UsernameTextBox.Text = AppSettings.Instance.LastUsername;

            if (IsRememberPassword)
                PasswordBox.Password = AppSettings.Instance.LastPassword;
        }
    }

    // 返回按钮事件处理
    private void ReturnButton_Click(object sender, RoutedEventArgs e)
    {
        // 如果是从标题栏调用的，关闭窗口
        if (!IsDuringStartup)
        {
            this.DialogResult = true;
            this.Close();
        }
        else
        {
            // 在启动过程中，切换回登录表单
            UserInfoPanel.Visibility = Visibility.Collapsed;
            LoginCard.Visibility = Visibility.Visible;
            AccountSelectionPanel.Visibility = Visibility.Collapsed;
        }
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // 保存当前的登录信息
        if(_currentUser != null)
        {
            if(!_currentUser.IsAdmin)
                AppSettings.Instance.LastUsername = UsernameTextBox.Text;
        }
        
    }

    private UserAccount? GetUserAccount(string username)
    {
        // 首先在用户账户中查找
        var user = AppSettings.Instance.UserAccounts.FirstOrDefault(u => u.Username == username);

        // 如果在常规账户中没找到，检查初始管理员账户
        if (user == null)
        {
            user = AppSettings.Instance.InitialAdminAccounts
                .FirstOrDefault(u => u.Username == username);

            if (user != null)
            {
                // 初始管理员账户不能添加到用户账户列表中
                //AppSettings.Instance.UserAccounts.Add(user);
                user.IsAdmin = true;
            }
        }
        return user;
    }

    private void LoginButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            string username = UsernameTextBox.Text.Trim();
            string password = PasswordBox.Password;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                ShowError("用户名和密码不能为空");
                return;
            }

            // 获取用户
            var user = GetUserAccount(username);
            if (user == null)
            {
                ShowError("用户名不存在");
                return;
            }

            // 验证密码
            string decryptedPassword;
            try
            {
                decryptedPassword = SecurityHelper.SimpleDecrypt(user.PasswordHash);
            }
            catch
            {
                ShowError("密码解密错误");
                return;
            }

            if (password != decryptedPassword)
            {
                ShowError("密码不正确");
                return;
            }

            // 记住密码设置
            if(IsRememberPassword)
            {
                if(user.IsAdmin)
                {
                    AppSettings.Instance.LastPassword = "";
                    AppSettings.Instance.LastUsername = "";
                    IsRememberPassword = false;
                    AppSettings.Instance.IsAutoLogin = false;
                }
                else
                {
                    // 保存最后一次密码输入
                    AppSettings.Instance.LastPassword = PasswordBox.Password;
                }
            }

            if (!user.IsAdmin)
            {
                // 保存最后一次登录信息
                AppSettings.Instance.LastUsername = username;
            }

            // 设置当前会话用户
            AppSettings.Instance.CurrentUser = user;
            _currentUser = user;

            // 登录成功后显示用户信息
            LoginSuccess?.Invoke(user);

            // 如果不是启动过程，则显示用户信息
            if (!IsDuringStartup)
            {
                // 延迟执行UI更新，确保事件处理完成
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    ShowUserInfo();
                }), System.Windows.Threading.DispatcherPriority.Background);
            }
        }
        catch (Exception ex)
        {
            // 记录异常或显示错误
            ShowError($"登录过程中发生错误: {ex.Message}");
        }
    }
    private void SwitchToRegesterForm()
    {
        LogoutFromSelectionButton.Visibility = Visibility.Collapsed;
        ReturnToLoginButton.Visibility = Visibility.Collapsed;
    }
    private void RegisterButton_Click(object sender, RoutedEventArgs e)
    {
        // 检查当前是否有管理员登录
        if (AppSettings.Instance.CurrentUser == null || !AppSettings.Instance.CurrentUser.IsAdmin)
        {
            ShowError("只有管理员账号可以注册新用户");
            return;
        }


        string username = RegUsernameTextBox.Text.Trim();
        string email = EmailTextBox.Text.Trim();
        string password = RegPasswordBox.Password;
        string confirmPassword = ConfirmPasswordBox.Password;
        SwitchToRegesterForm();

        if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(email) ||
            string.IsNullOrEmpty(password) || string.IsNullOrEmpty(confirmPassword))
        {
            ShowError("所有字段都必须填写");
            return;
        }

        if (password != confirmPassword)
        {
            ShowError("两次输入的密码不一致");
            return;
        }

        if (AppSettings.Instance.UserAccounts.Any(u => u.Username == username))
        {
            ShowError("用户名已存在");
            return;
        }

        if (AppSettings.Instance.UserAccounts.Any(u => u.Email == email))
        {
            ShowError("邮箱已被注册");
            return;
        }

        // 创建新用户
        var newUser = new UserAccount
        {
            Username = username,
            Email = email,
            PasswordHash = SecurityHelper.SimpleEncrypt(password) // 加密存储
        };

        AppSettings.Instance.UserAccounts.Add(newUser);

        // 注册成功后切换到登录表单，而不是自动登录
        SwitchToLoginForm();

        // 清空注册表单
        RegUsernameTextBox.Text = "";
        EmailTextBox.Text = "";
        RegPasswordBox.Password = "";
        ConfirmPasswordBox.Password = "";

        // 注册成功后不清除表单，保持注册状态
        // 显示注册成功消息，但不自动跳转
        ShowError("注册成功，您可以继续注册或返回");

        // 添加返回用户信息界面的按钮
        var returnToUserInfoButton = new Button
        {
            Content = "返回用户信息",
            Style = (Style)FindResource("SecondaryButtonStyle"),
            Margin = new Thickness(0, 10, 0, 0)
        };
        returnToUserInfoButton.Click += (s, args) => {
            // 移除临时添加的按钮
            RegisterForm.Children.Remove(returnToUserInfoButton);
            // 返回用户信息界面
            SwitchToLoginForm();
            ShowUserInfo();
        };

        // 添加返回按钮到注册表单
        if (!RegisterForm.Children.Contains(returnToUserInfoButton))
        {
            RegisterForm.Children.Add(returnToUserInfoButton);
        }
    }

    // 显示账户选择时更新按钮状态
    public void ShowAccountSelection()
    {
        LoginCard.Visibility = Visibility.Collapsed;
        UserInfoPanel.Visibility = Visibility.Collapsed;
        AccountSelectionPanel.Visibility = Visibility.Visible;

        // 获取当前用户
        var currentUser = _currentUser ?? AppSettings.Instance.CurrentUser;
        bool isAdmin = currentUser != null && currentUser.IsAdmin;

        // 加载账户列表
        var accounts = AppSettings.Instance.UserAccounts;
        AccountListBox.ItemsSource = accounts;

        // 设置当前已登录的账户为选中状态
        if (currentUser != null)
        {
            var currentAccount = accounts.FirstOrDefault(acc => acc.Username == currentUser.Username);
            if (currentAccount != null)
            {
                AccountListBox.SelectedItem = currentAccount;
            }
        }

        // 更新按钮状态
        UpdateAccountSelectionButtons();

        // 根据用户类型调整界面
        if (isAdmin)
        {
            // 管理员可以看到移除按钮和其他账户按钮
            RemoveAccountButton.Visibility = Visibility.Visible;
            // 管理员不能注销自己，隐藏注销按钮
            LogoutFromSelectionButton.Visibility = Visibility.Collapsed;
            LogoutButtonInLoginForm.Visibility = Visibility.Collapsed;
        }
        else
        {
            // 普通用户不能移除账户
            RemoveAccountButton.Visibility = Visibility.Collapsed;
        }
    }
    // 更新登录界面的按钮状态
    private void UpdateLoginFormButtons()
    {
        bool isLoggedIn = AppSettings.Instance.CurrentUser != null;

        // 创建或查找登录表单中的返回登录和注销按钮
        var returnButton = FindName("ReturnToLoginButtonInLoginForm") as Button;
        var logoutButton = FindName("LogoutButtonInLoginForm") as Button;

        // 如果按钮不存在，可能需要先创建它们
        if (returnButton == null)
        {
            returnButton = new Button
            {
                Name = "ReturnToLoginButtonInLoginForm",
                Content = "返回登录",
                Style = (Style)FindResource("TextButtonStyle"),
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(10, 10, 0, 0),
                Visibility = Visibility.Collapsed
            };
            returnButton.Click += ReturnToLoginFromLoginForm_Click;
            // 添加到登录表单的适当位置
            // 这里需要根据你的实际布局调整
            LoginForm.Children.Insert(0, returnButton);
        }

        if (logoutButton == null)
        {
            logoutButton = new Button
            {
                Name = "LogoutButtonInLoginForm",
                Content = "注销",
                Style = (Style)FindResource("TextButtonStyle"),
                HorizontalAlignment = HorizontalAlignment.Right,
                Foreground = Brushes.Red,
                Margin = new Thickness(0, 10, 10, 0),
                Visibility = Visibility.Collapsed
            };
            logoutButton.Click += LogoutFromLoginForm_Click;
            // 添加到登录表单的适当位置
            // 这里需要根据你的实际布局调整
            LoginForm.Children.Insert(0, logoutButton);
        }

        // 根据登录状态设置按钮可见性
        returnButton.Visibility = isLoggedIn ? Visibility.Visible : Visibility.Collapsed;
        logoutButton.Visibility = isLoggedIn ? Visibility.Visible : Visibility.Collapsed;
    }
    // 登录表单中的返回登录按钮点击事件
    private void ReturnToLoginFromLoginForm_Click(object sender, RoutedEventArgs e)
    {
        // 返回到用户信息界面
        LoginCard.Visibility = Visibility.Collapsed;
        ShowUserInfo();
    }
    // 登录表单中的注销按钮点击事件
    private void LogoutFromLoginForm_Click(object sender, RoutedEventArgs e)
    {
        Logout();
        // 更新按钮状态
        UpdateLoginFormButtons();
    }
    // 在显示登录表单时更新按钮状态
    private void SwitchToLoginForm()
    {
        LoginForm.Visibility = Visibility.Visible;
        UserInfoPanel.Visibility = Visibility.Collapsed;
        RegisterForm.Visibility = Visibility.Collapsed;
        RegisterScrollViewer.Visibility = Visibility.Collapsed;
        TitleText.Text = "登录";
        ClearForm();

        // 更新登录表单的按钮状态
        UpdateLoginFormButtons();

        // 设置焦点到用户名输入框
        Dispatcher.BeginInvoke(new Action(() =>
        {
            UsernameTextBox.Focus();
        }), System.Windows.Threading.DispatcherPriority.Render);
    }
    // 更新账户选择界面按钮状态
    private void UpdateAccountSelectionButtons()
    {
        bool hasAccounts = AppSettings.Instance.UserAccounts.Any();
        bool isLoggedIn = AppSettings.Instance.CurrentUser != null;

        // 设置按钮可见性
        SelectAccountButton.IsEnabled = hasAccounts && AccountListBox.SelectedItem != null;
        RemoveAccountButton.IsEnabled = hasAccounts && AccountListBox.SelectedItem != null;

        // 设置返回和注销按钮的可见性
        ReturnToLoginButton.Visibility = isLoggedIn ? Visibility.Visible : Visibility.Collapsed;
        LogoutFromSelectionButton.Visibility = isLoggedIn ? Visibility.Visible : Visibility.Collapsed;
    }

    // 账户选择事件
    private void AccountListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // 更新按钮状态
        UpdateAccountSelectionButtons();
    }

    // 选择账户按钮点击
    private void SelectAccountButton_Click(object sender, RoutedEventArgs e)
    {
        if (AccountListBox.SelectedItem is UserAccount selectedAccount)
        {
            // 自动填充登录信息
            UsernameTextBox.Text = selectedAccount.Username;
            PasswordBox.Password = SecurityHelper.SimpleDecrypt(selectedAccount.PasswordHash);

            // 切换到登录表单
            SwitchToLoginForm();
            AccountSelectionPanel.Visibility = Visibility.Collapsed;
            LoginCard.Visibility = Visibility.Visible;
        }
    }

    // 移除账户按钮点击
    private void RemoveAccountButton_Click(object sender, RoutedEventArgs e)
    {
        if (AccountListBox.SelectedItem is UserAccount selectedAccount)
        {
            var result = MessageBox.Show($"确定要移除账户 {selectedAccount.Username} 吗？",
                "确认移除", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                AppSettings.Instance.UserAccounts.Remove(selectedAccount);

                // 如果移除的是当前登录的账户，则注销
                if (AppSettings.Instance.CurrentUser != null &&
                    AppSettings.Instance.CurrentUser.Username == selectedAccount.Username)
                {
                    Logout();
                }

                // 检查是否移除了所有账户
                if (!AppSettings.Instance.UserAccounts.Any())
                {
                    // 确保清除记住的账户
                    AppSettings.Instance.CurrentUser = null;
                    _currentUser = null;

                    // 切换到登录表单
                    UserInfoPanel.Visibility = Visibility.Collapsed;
                    AccountSelectionPanel.Visibility = Visibility.Collapsed;
                    LoginCard.Visibility = Visibility.Visible;
                    SwitchToLoginForm();

                    // 触发注销事件
                    LoginSuccess?.Invoke(null);
                }

                // 刷新列表
                AccountListBox.ItemsSource = null;
                AccountListBox.ItemsSource = AppSettings.Instance.UserAccounts;

                // 更新按钮状态
                UpdateAccountSelectionButtons();
            }
        }
    }

    // 其他账户按钮点击
    private void OtherAccountButton_Click(object sender, RoutedEventArgs e)
    {
        AccountSelectionPanel.Visibility = Visibility.Collapsed;
        LoginCard.Visibility = Visibility.Visible;
        SwitchToLoginForm(); // 确保切换到登录表单
    }

    // 从账户选择界面返回登录
    private void ReturnToLoginButton_Click(object sender, RoutedEventArgs e)
    {
        AccountSelectionPanel.Visibility = Visibility.Collapsed;
        ShowUserInfo(); // 显示用户信息而不是登录表单
        if (IsRememberPassword)
            PasswordBox.Password = AppSettings.Instance.LastPassword;
    }

    // 从账户选择界面注销
    private void LogoutFromSelectionButton_Click(object sender, RoutedEventArgs e)
    {
        Logout();
        UpdateAccountSelectionButtons(); // 更新按钮状态
    }

    private void SwitchToRegisterButton_Click(object sender, RoutedEventArgs e)
    {
        SwitchToRegisterForm();
    }

    // 从用户信息切换账户
    private void SwitchAccountFromInfoButton_Click(object sender, RoutedEventArgs e)
    {
        // 获取当前用户
        var currentUser = _currentUser ?? AppSettings.Instance.CurrentUser;

        if (currentUser != null && currentUser.IsAdmin)
        {
            // 管理员进入账户选择界面
            UserInfoPanel.Visibility = Visibility.Collapsed;
            // 设置注销按钮不可见
            LogoutButtonInLoginForm.Visibility = Visibility.Collapsed;

            ShowAccountSelection();
        }
        else
        {
            // 普通用户直接返回登录界面
            UserInfoPanel.Visibility = Visibility.Collapsed;
            LoginCard.Visibility = Visibility.Visible;
            AccountSelectionPanel.Visibility = Visibility.Collapsed;

            // 设置返回登录和注销按钮可见
            ReturnToLoginButtonInLoginForm.Visibility = Visibility.Visible;
            LogoutButtonInLoginForm.Visibility = Visibility.Visible;
        }
    }

    // 添加注销按钮事件处理
    private void LogoutButton_Click(object sender, RoutedEventArgs e)
    {
        Logout();
    }

    // 统一的注销方法
    // 统一的注销方法
    private void Logout()
    {
        // 获取当前用户
        var currentUser = _currentUser ?? AppSettings.Instance.CurrentUser;

        // 管理员不能注销自己，只能切换账户
        if (currentUser != null && currentUser.IsAdmin)
        {
            // 管理员切换到账户选择界面而不是注销
            UserInfoPanel.Visibility = Visibility.Collapsed;
            ShowAccountSelection();
            return;
        }

        // 普通用户执行注销操作
        // 清除当前会话用户
        AppSettings.Instance.CurrentUser = null;
        _currentUser = null;

        // 切换到登录表单而不是账户选择
        UserInfoPanel.Visibility = Visibility.Collapsed;
        AccountSelectionPanel.Visibility = Visibility.Collapsed;
        LoginCard.Visibility = Visibility.Visible;
        SwitchToLoginForm();

        // 触发注销事件
        LoginOut?.Invoke();
    }

    // 切换账户按钮事件处理
    private void SwitchToLoginButton_Click(object sender, RoutedEventArgs e)
    {
        // 显示账户选择
        ShowAccountSelection();
    }

    private void SwitchToRegisterForm()
    {
        // 隐藏其他面板
        UserInfoPanel.Visibility = Visibility.Collapsed;
        AccountSelectionPanel.Visibility = Visibility.Collapsed;

        // 确保登录卡片可见（包含注册表单）
        LoginCard.Visibility = Visibility.Visible;

        // 显示注册表单，隐藏登录表单
        LoginForm.Visibility = Visibility.Collapsed;
        RegisterForm.Visibility = Visibility.Visible;
        RegisterScrollViewer.Visibility = Visibility.Visible;
        TitleText.Text = "注册";
        ClearForm();

        // 设置焦点到用户名输入框
        Dispatcher.BeginInvoke(new Action(() =>
        {
            RegUsernameTextBox.Focus();
        }), System.Windows.Threading.DispatcherPriority.Render);

        // 重置滚动位置
        RegisterScrollViewer.ScrollToVerticalOffset(0);
    }

    private void ClearForm()
    {
        ErrorContainer.Visibility = Visibility.Collapsed;
    }

    private void ShowError(string message)
    {
        ErrorText.Text = message;
        ErrorContainer.Visibility = Visibility.Visible;
    }

    // 添加窗口拖动功能
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);
        DragMove();
    }

    // 关闭按钮点击事件 - 修复逻辑
    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        // 检查是否有用户登录
        if (IsDuringStartup || AppSettings.Instance.CurrentUser==null)
        {
            // 启动过程中关闭登录窗口则退出整个应用
            Application.Current.Shutdown();
        }
        else
        {
            // 正常使用过程中只关闭登录窗口
            this.DialogResult = false;
            Close();
        }
    }

    // 添加ESC键关闭支持
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Escape)
        {
            if (IsDuringStartup)
            {
                Application.Current.Shutdown();
            }
            else
            {
                this.DialogResult = false;
                Close();
            }
            e.Handled = true;
        }
        base.OnKeyDown(e);
    }
    // 在 LoginWindow 类中添加以下方法
    private void SyncLoginOptions()
    {
        // 如果启用自动登录，必须同时启用记住密码
        if (IsAutoLogin && !IsRememberPassword)
        {
            IsRememberPassword = true;
            RememberMeCheckBox.IsChecked = true;
        }
    }

    // 在 AutoLoginCheckBox 的 Checked 和 Unchecked 事件处理程序中调用
    private void AutoLoginCheckBox_Checked(object sender, RoutedEventArgs e)
    {
        SyncLoginOptions();
    }

    private void AutoLoginCheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        // 取消自动登录时，不需要强制取消记住密码
    }

}
